<template>
    <el-container class="login-container">
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm"
            :size="formSize" status-icon>
            <el-form-item label="用户名" prop="userName">
                <el-input 
                v-model="ruleForm.userName"
                placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input 
                v-model="ruleForm.password" 
                type="password"
                placeholder="请输入密码"
                show-password
                />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    确定
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </el-container>
</template>
  
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus'

const router = useRouter();

interface RuleForm {
    userName: string,
    password: string,
}

const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
    userName: 'admin',
    password: '123456'
})

const rules = reactive<FormRules<RuleForm>>({
    userName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 50, message: '长度限制3~60(包含)', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '长度限制6~20(包含)', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!', formEl, ruleForm)
            if (ruleForm.userName === 'admin' && ruleForm.password === '123456') {
                ElMessage({
                    message: '登陆成功',
                    type: 'success',
                })
                router.push('/SoftList')
            } else {
                ElMessage.error('用户名或密码错误')
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

</script>


<style scoped>
.login-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f0f2f5;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;

}
</style>

<!-- 
        top: 0;
    left: 0;
    right: 0;
    bottom: 0; // 不加有滚动条

    border: 1px solid #ddd;
    margin: auto; // 没啥用？

 -->